package com.okoj.excel_lib_rary.data;

import android.text.TextUtils;
import android.util.Log;

import com.okoj.excel_lib_rary.config.Config;
import com.okoj.excel_lib_rary.data.entity.Kind;
import com.okoj.excel_lib_rary.data.entity.RemoteFile;
import com.okoj.excel_lib_rary.data.entity.Result;
import com.okoj.excel_lib_rary.data.entity.Template;
import com.okoj.excel_lib_rary.data.entity.WpsCommon;
import com.okoj.excel_lib_rary.data.net.WpsClient;
import com.okoj.excel_lib_rary.listener.LoadDataListener;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

import static com.okoj.excel_lib_rary.util.FileUtil.generateRandomViewCountString;

public class RemoteResp {

    public static void getAllKind(LoadDataListener<List<Kind>> loadDataListener){
        String TAG = "getAllKind";
        loadDataListener.onStart();
        Config config = WpsClient.getAppConfig();
        WpsClient.getInstance().wpsService()
                .getAllKinds(config.getAppid())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(result -> {
                    Log.i(TAG,result.toString());
                    return result.getData();
                })
                .subscribe(new Observer<List<Kind>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull List<Kind> kinds) {
                        if(kinds!=null && kinds.size()>1){
                            loadDataListener.onSuccess(kinds);
                        }else {
                            loadDataListener.onError("数据为空");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        Log.e(TAG,"onError:"+e.getMessage());
                        loadDataListener.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public static void getTemplateByKind(String kindId,LoadDataListener<List<Template.SonFilesBean>> loadDataListener){
        String TAG = "getTemplateByKind";
        String appid = WpsClient.getAppConfig().getAppid();
        loadDataListener.onStart();
        WpsClient.getInstance().wpsService()
                .getAllTemplateByKind(kindId,appid)
                .map(result -> {
                    // 浏览人数，使用本地随机生成的
                    if(result == null
                            || result.getData() == null
                            || result.getData().getSon_files() == null
                            || result.getData().getSon_files().size()<=0){
                    }else {
                        for (Template.SonFilesBean filesBean:result.getData().getSon_files()) {
                            filesBean.setViewCountString(generateRandomViewCountString());
                            if(TextUtils.isEmpty(filesBean.getFilemodulemodel().getOss_file())){
                                Log.e(TAG,"id:"+ filesBean.getId());
                            }
                            if(!TextUtils.isEmpty(filesBean.getFilemodulemodel().getCover())){
                                Log.e(TAG,"cover:"+ filesBean.getFilemodulemodel().getCover());
                            }
                        }
                    }

                    return result;
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<Template>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {


                    }

                    @Override
                    public void onNext(@NonNull Result<Template> result) {

                        Log.i(TAG,result.toString());
                        if(result == null
                                || result.getData() == null
                                || result.getData().getSon_files() == null
                                || result.getData().getSon_files().size()<=0){
                            loadDataListener.onError("数据为空");
                        }else {
                            loadDataListener.onSuccess(result.getData().getSon_files());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                        Log.e(TAG,e.getMessage());

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public static void previewTemplate(String templateId,String fileType,LoadDataListener<WpsCommon> loadDataListener){
        Config config = WpsClient.getAppConfig();
        WpsClient.getInstance()
                .wpsService()
                .previewTemplate(config.getToken(),
                        config.getAppid(),config.get_w_env(),
                        config.get_w_platform(),templateId,fileType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<WpsCommon>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull Result<WpsCommon> result) {

                        if (result.getData() == null ){
                            loadDataListener.onError("空数据");
                        }else {
                            loadDataListener.onSuccess(result.getData());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        loadDataListener.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public static void editPublicTemplate(String fileId,String fileType,LoadDataListener<WpsCommon> loadDataListener){
        Config config = WpsClient.getAppConfig();
        WpsClient.getInstance().wpsService()
                .editPublicModule(config.getToken(),fileId,
                        config.getAppid(),config.get_w_env()
                        ,config.get_w_platform(),fileType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<WpsCommon>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result<WpsCommon> result) {
                        if(result.getData() == null || result.getCode()!= 0){
                            loadDataListener.onError(result.getMessage());
                        }else {
                            loadDataListener.onSuccess(result.getData());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        loadDataListener.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public static void getPersonFile(LoadDataListener<List<RemoteFile>> loadDataListener){
        String TAG = "getPersonFile";
        Config config = WpsClient.getAppConfig();
        loadDataListener.onStart();
        WpsClient.getInstance()
                .wpsService()
                .getUserFileList(config.getToken(),config.getAppid(),config.get_w_env(),config.get_w_platform())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<List<RemoteFile>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull Result<List<RemoteFile>> result) {
                        Log.i(TAG,"getPersonFile:"+result);
                        if(result == null){
                            loadDataListener.onError("null");
                        }{
                            loadDataListener.onSuccess(result.getData());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        loadDataListener.onError(e.getMessage());
                        Log.e(TAG,"getPersonFile:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
